// 路由文件
import { createRouter, createWebHistory } from "vue-router";

import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Workspace from '../views/ProjectWorkspace.vue'

const routes = [
    {
        path: '/',
        name: 'Home',
        component: Home,
        children:[
            {
                path: '/',
                name: 'Index',
                component: () => import('../views/Index.vue')
            },{
                path: '/setting',
                name: 'Setting',
                component: () => import('../views/Setting.vue')
            },{
                path: '/help',
                name: 'Help',
                component: () => import('../views/Help.vue')
            }
        ],
        meta:{
            requireAuth: true
        }
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/workspace/:projectId',
        name: 'ProjectWorkspace',
        component: Workspace,
        props: true,
        children:[
            {
                path: '',
                name: 'ProjectFile',
                component: () => import('../views/ProjectFile.vue'),
                props: true
            },
            {
                path: '/stationConfig/:projectId',
                name: 'StationConfig',
                component: () => import('../views/StationConfig.vue'),
                props: true
            },
            {
                path: '/routingSetting/:projectId',
                name: 'RoutingSetting',
                component: () => import('../views/RoutingSetting.vue'),
                props: true
            },
            {
                path: '/generate/:projectId/:routingId',
                name: 'GenerateRouting',
                component: () => import('../views/GenerateRouting.vue'),
                props: true
            }
        ],
        meta:{
            requireAuth: true
        }
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})


router.beforeEach((to,from)=>{
    if(to.meta.requireAuth) {
        let token = localStorage.getItem('routing-token');
        let isLogin = localStorage.getItem('routing-token');
        if(!token||!isLogin){
            return {
                path: '/login'
            }
        }
    }
})

export default router;
